﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KPIS.GERP.GBMS.APP;
using KPIS.GERP.GBMS.IBLL;
using KPIS.GERP.GBMS.BLL;
using KPIS.GERP.GBMS.MODEL;
using System.Configuration;
using System.Data;
using Telerik.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Globalization;

namespace KPIS.GERP.GBMS.APP_UI.Form
{
    public partial class BGM_Strategy_Plan : System.Web.UI.Page, ICallbackEventHandler
    {
        private DataTable DT;
        private IStrategyPlanBLL bal = new StrategyPlanBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            RegisterCallBackReference();

            if (!IsPostBack)
            {
                iniYear();
                tView.Nodes.Clear();
                int tYear = Convert.ToInt32(cbYear.SelectedValue);//SystemInfo._CurrentYear;
                //int tYear = YearCombo1.;
                lbCurYear.Value = tYear.ToString();
                lbYear.Text = tYear.ToString();
                this.DT = bal.GetStrategyPlanByYear(tYear);
                tView.DataSource = this.DT;
                tView.DataBind();
                tView.ExpandAllNodes();
                Session["DT"] = this.DT;
            }
            else
            {         
                this.DT = (DataTable)Session["DT"];
                RefreshDisplay();
            }

            //YearCombo1.PostBack = true;
            //((RadComboBox)YearCombo1.FindControl("year")).Width = 50;
            //YearCombo1.indexChange += new RadComboBoxSelectedIndexChangedEventHandler(OnSelectedIndexChanged);

        }

        protected void RefreshDisplay()
        {
            tView.Nodes.Clear();
            int tYear = int.Parse(cbYear.SelectedValue); 
            //Convert.ToInt32(((RadComboBox)YearCombo1.FindControl("year")).SelectedValue);
            lbYear.Text = tYear.ToString();
            this.DT = bal.GetStrategyPlanByYear(tYear);
            tView.DataSource = DT;
            tView.DataBind();
            tView.ExpandAllNodes();
        }

        protected void btn_AddChild_Click(object sender, EventArgs e)
        {
            if (tView.SelectedNode != null)
            {
                Telerik.Web.UI.RadTreeNode tNode = new Telerik.Web.UI.RadTreeNode(DateTime.Now.ToString("HH:mm:ss:"));
                tView.SelectedNode.Nodes.Add(tNode);
            }
        }

        protected void btn_DelNode_Click(object sender, EventArgs e)
        {
            StrategyPlanBLL SPB = new StrategyPlanBLL();
            SPB.Delete(Convert.ToInt32(hdNodeSeq.Value));
            RefreshDisplay();
            tView.Nodes.Clear();
            tView.DataSource = this.DT;
            tView.DataBind();
            tView.ExpandAllNodes();
            Alert("ลบข้อมูลเรียบร้อย");
        }

        [System.Web.Services.WebMethod()]
        public static void TestReturnString()
        {
           
        }

        #region "Call back Registration"
        private void RegisterCallBackReference()
        {
            String callBack = Page.ClientScript.GetCallbackEventReference(this, "arg",
                 "CallbackOnSucceeded", "context", "CallbackOnFailed", true);
            //String callBack = Page.ClientScript.GetCallbackEventReference(this, "arg",
            //   "", "context", "", true);
            String clientFunction = "function CallServer(arg, context){ "
                           + callBack + "; }";
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
                    "Call To Server", clientFunction, true);
        }
        #endregion


        #region ICallbackEventHandler Members
        String returnValue;
        string ICallbackEventHandler.GetCallbackResult()
        {
            return returnValue;
        }

        void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)
        {
            Page_Load(null, null);
            //Test();
            returnValue = "";
        }


        #endregion

        //protected void OnSelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        //{
        //    string x = "a";
        //}

        private void Alert(string Message)
        {
            string js = "";
            js = "alert('" + Message + "');";
            if (ScriptManager.GetCurrent(base.Page).IsInAsyncPostBack)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", js, true);
            }
            else
            {
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), "Message", js, true);
            }
        }

        //protected void btn_Clone_Click(object sender, EventArgs e)
        //{
        //    //RadComboBox Combo = (RadComboBox)YearCombo1.FindControl("Year");

        //    if (cbYear.SelectedValue.ToString() == lbCurYear.Value)
        //    {
        //        Alert("ไม่สามารถคัดลอกข้อมูลของปีเดียวกันได้");
        //        return;
        //    }
        //    StrategyPlanBLL SPB = new StrategyPlanBLL();
        //    string tmp = SPB.CloneStrategyPlanByYear(Convert.ToInt32(cbYear.SelectedValue.ToString()), Convert.ToInt32(lbCurYear.Value), 1);
        //    if (tmp == "OK")
        //    {
        //        RefreshDisplay();
        //        tView.Nodes.Clear();
        //        tView.DataSource = this.DT;
        //        tView.DataBind();
        //        tView.ExpandAllNodes();
        //        Alert("คัดลอกข้อมูลเรียบร้อย");
        //    }
        //    else if (tmp == "NoData")
        //    {
        //        Alert("ไม่สามารถคัดลอกข้อมูลของปี " + cbYear.SelectedValue.ToString() + " ได้เนื่องจากไม่มีข้อมูล");
        //    }
        //    else if (tmp == "CanNotDelete")
        //    {
        //        Alert("เกิดข้อผิดพลาด ไม่สามารถลบข้อมูลของปี " + lbYear.Text + " ได้");
        //    }
        //    else if (tmp == "Error")
        //    {
        //        Alert("เกิดข้อผิดพลาด ไม่สามารถทำการคัดลอกข้อมูลได้");
        //    }
        //}

        protected void cbYear_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            this.DT = (DataTable)Session["DT"];
            Session["Year"] = cbYear.SelectedValue;
            RefreshDisplay();
        }

        private void iniYear()
        {
            int CurrYear = SystemInfo._CurrentYear;
            cbYear.Items.Clear();
            for (int i = CurrYear + 5; i >= CurrYear - 5; i--)
            {
                cbYear.Items.Add(new RadComboBoxItem(i.ToString(), i.ToString()));
            }

            if (Session["Year"] != null)
            {
                cbYear.SelectedValue = Session["Year"].ToString();
            }
            else
            {
                cbYear.SelectedValue = CurrYear.ToString();
            }
        }

        protected void btn_Approve_Click(object sender, EventArgs e)
        {
            bal.Approve(cbYear.SelectedValue, new SsMng().EmpSeq);
        }
    }
}
